﻿package nl.stroep.classbuilder.app
{
	import fl.controls.ComboBox;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	import nl.stroep.classbuilder.cb.CbClass;
	import nl.stroep.classbuilder.cb.CbMethod;
	import nl.stroep.classbuilder.cb.CbParameter;
	import nl.stroep.classbuilder.cb.CbVariable;
	import nl.stroep.classbuilder.codeformatter.CodeFormatter;
	import nl.stroep.classbuilder.definitions.AccessModifiers;
	import nl.stroep.classbuilder.definitions.DataTypes;
	import nl.stroep.classbuilder.definitions.VariableModifiers;
	import nl.stroep.classbuilder.packages.Package;
	import nl.stroep.classbuilder.project.Project;
	import nl.stroep.classbuilder.project.ProjectManager;
	
	/**
	 * ...
	 * @author Mark Knol
	 */
	public final class Main extends Sprite 
	{
		private var myProject:Project;
		private var projectManager:ProjectManager;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			
			projectManager = ProjectManager.getInstance();
			projectManager.addEventListener(Event.COMPLETE, onProjectLoaded);
			
			myProject = new Project("myProject");
			myProject.settings.author = "Mark Knol"
			
			projectManager.add(myProject);
			
			createEventTest();
			
			var projectDD:ComboBox = new ComboBox();
			for (var i:int = 0; i < projectManager.projects.length; ++i) 
			{
				projectDD.addItem( { label: projectManager.projects[i].name  } );
			}
			
			this.addChild(projectDD);
		}
		
		private function onProjectLoaded(e:Event):void 
		{
			
			var textfield:TextField = new TextField()
			textfield.defaultTextFormat = new TextFormat("Consolas", 11, 0, false);
			textfield.condenseWhite = true;
			textfield.text = CodeFormatter.format( projectManager.current.classes[0].toString() );
			textfield.width = stage.stageWidth * 0.5;
			textfield.height = textfield.textHeight + 10;
			textfield.x = stage.stageWidth * 0.5;
			textfield.wordWrap = true;
			this.addChild(textfield);
		}
		
		private function createEventTest():void
		{
			var p1:Package = new Package("nl");			
			var p2:Package = new Package("stroep", p1);
			var p3:Package = new Package("example", p2);
			var p4:Package = new Package("events", p3);
			
			var startTime:Number = getTimer();
			
			var myStaticVar1:CbVariable = new CbVariable("ADDED", DataTypes.STRING, "added", AccessModifiers.PUBLIC, VariableModifiers.STATIC, true);
			var myStaticVar2:CbVariable = new CbVariable("LOADED", DataTypes.STRING, "loaded", AccessModifiers.PUBLIC, VariableModifiers.STATIC, true);
			
			var params:Vector.<CbParameter> = new Vector.<CbParameter>();
			params.push( new CbParameter("id", DataTypes.INT, null, true, true ) )
			params.push( new CbParameter("label", DataTypes.STRING, null, true, false ) );
			
			params.push( new CbParameter("type", DataTypes.STRING, null, false, false ) );
			params.push( new CbParameter("bubbles", DataTypes.BOOLEAN, "false", false, false ) );
			params.push( new CbParameter("cancelable", DataTypes.BOOLEAN, "false", false, false ) );
			
			var myFunc1:CbMethod = new CbMethod("clone", DataTypes.VOID, null, AccessModifiers.PUBLIC, null, "return new MyEvent(id,label,type,bubles,cancelable);" );
			
			var myClassVariables:Vector.<CbVariable> = new Vector.<CbVariable>();
			myClassVariables.push(myStaticVar1)
			myClassVariables.push(myStaticVar2)
			
			var myFunctions:Vector.<CbMethod> = new Vector.<CbMethod>();
			myFunctions.push(myFunc1)
			
			var cbEventClass:CbClass = new CbClass("MyEvent", myFunctions, myClassVariables, params, DataTypes.EVENT, "IEventDispatcher", null, p4.path, myProject.settings.author);
			
			
			trace("Calculated event in ", getTimer() - startTime, "ms");
			
			
			myProject.addClass( cbEventClass );
		}
		
		
	}
	
}